package timingAttack;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ServerHandler implements Runnable{

	private Socket connection;

	public ServerHandler(Socket connection){
		this.connection = connection;

	}

	public static boolean equals(byte[] digest1, byte[] digest2) {
		for (int i = 0; i < digest2.length; ++i /*i= incr(i)*/) {
			if(digest1[i] != digest2[i])
				return false;
		}
		return true;
	}

	public void run(){
		try {
			InputStream in = connection.getInputStream();
			OutputStream out = connection.getOutputStream();
			byte[] buffer = new byte[16];
			while(true){
				in.read(buffer);
				if(equals(Server.digest, buffer)){
					out.write(1);
					out.flush();
				}
				else{
					out.write(0);
					out.flush();
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
